//
//  HLEditNicknameViewController.m
//  YueYue
//
//  Created by hulu001 on 2023/5/19.
//

#import "HLEditNicknameViewController.h"

@interface HLEditNicknameViewController ()<QMUITextFieldDelegate>

@property (nonatomic, strong) QMUIButton *saveButton;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) QMUITextField *textField;
@property (nonatomic, strong) NSString *nickname;
@property (nonatomic, copy) HLCommonCallbackStringBlock callback;

@end

@implementation HLEditNicknameViewController

- (instancetype)initWithNickname:(NSString *)nickname callback:(HLCommonCallbackStringBlock)callback
{
    self = [super init];
    if (self){
        self.nickname = nickname;
        self.callback = callback;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = UIColorWhite;
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self.textField becomeFirstResponder];
}

- (void)setupNavigationItems
{
    [super setupNavigationItems];
    
    UIBarButtonItem *saveItem = [[UIBarButtonItem alloc] initWithCustomView:({
        QMUIButton *saveButton = [[QMUIButton alloc] init];
        [saveButton setTitle:@"保存" forState:UIControlStateNormal];
        [saveButton setTitleColor:HLTextColor3 forState:UIControlStateNormal];
        [saveButton.titleLabel setFont:UIFontMediumMake(12)];
        [saveButton setBackgroundColor:HLBackgroundColor];
        [saveButton setContentEdgeInsets:UIEdgeInsetsMake(6, 12, 6, 12)];
        saveButton.cornerRadius = 14;
        [saveButton addTarget:self action:@selector(saveAction) forControlEvents:UIControlEventTouchUpInside];
        self.saveButton = saveButton;
        saveButton;
    })];
    
    self.navigationItem.rightBarButtonItem = saveItem;
}

- (void)initSubviews
{
    [super initSubviews];
    self.titleLabel = [[UILabel alloc] init];
    _titleLabel.font = UIFontMediumMake(15);
    _titleLabel.textColor = HLTextColor1;
    _titleLabel.text = @"编辑昵称";
    [self.view addSubview:_titleLabel];
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(16);
        make.top.mas_equalTo(NavigationContentTopConstant + 12);
    }];
    
    self.textField = [[QMUITextField alloc] init];
    _textField.placeholder = @"请填写您修改的昵称";
    _textField.font = UIFontMake(14);
    _textField.textColor = HLTextColor1;
    _textField.backgroundColor = HLBackgroundColor;
    _textField.textInsets = UIEdgeInsetsMake(0, 16, 0, 16);
    _textField.delegate = self;
    _textField.maximumTextLength = 16;
    [self.view addSubview:_textField];
    [_textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.equalTo(_titleLabel.mas_bottom).offset(12);
        make.height.mas_equalTo(60);
    }];
    
    if (self.nickname.length > 0){
        _textField.text = self.nickname;
    }
}

- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleDarkContent;
}

- (UIImage *)qmui_navigationBarBackgroundImage
{
    return [UIImage qmui_imageWithColor:UIColorWhite];
}

- (nullable UIColor *)qmui_navigationBarTintColor
{
    return UIColorBlack;
}

- (void)saveAction
{
    if (self.textField.text.length < 2){
        [QMUITips showWithText:@"昵称必须大于2个字符"];
        return;
    }
    
    if (self.textField.text.length > 30){
        [QMUITips showWithText:@"昵称必须小于30个字符"];
        return;
    }
    
    if (self.callback){
        self.callback(self.textField.text);
    }
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string originalValue:(BOOL)originalValue
{
    if (textField.text.length > 2){
        [self.saveButton setBackgroundColor:HLMainColor];
        [self.saveButton setTitleColor:UIColorWhite forState:UIControlStateNormal];
    } else {
        [self.saveButton setTitleColor:HLTextColor3 forState:UIControlStateNormal];
        [self.saveButton setBackgroundColor:HLBackgroundColor];
    }
    
    return YES;
}

@end
